Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Figure 11.4. The completed menu displayed in the Menu Editor. #### Other Menu Editor Options The Menu Editor contains checkboxes for setting several menu item properties. These properties control how the menu item is displayed when the program is running and the user pulls down the menu: • Checked—Displays a check mark next to the menu item when the menu is displayed. • Enabled—Indicates that a menu item is available. If a menu item is disabled, it is grayed out, indicating it is unavailable to the user. • Visible—Toggles the display of a menu item. You can hide a menu command by setting its Visible property to False. You can also edit the structure of a menu as follows: • Click on the up or down arrow to move the current menu item (the one highlighted in the hierarchical list) without changing its left-right position. • Click on the left or right arrow to change a menu item’s position in the hierarchy. You can have as many as four levels in a menu system. • Click on the Insert button to add a new, blank menu item above the current item. • Click on the Delete button to delete the current menu item. A few items in the Menu Editor remain to be covered. I’ll let you explore them on your own. ### Programming The Editor, Part 2 With the two controls placed and the menus designed, we have completed the visual design part of the Baby Editor project. Now we are ready to turn our attention to the code. Let’s start with the general declarations section, which is where we place the declarations of global variables and constants—those variables and constants that must be available in all of the module’s procedures. We require two type Boolean flags, plus two type String variables. The general declarations code is shown in Listing 11.1. In case you don’t remember how to display a specific part of a project’s code, let me refresh your memory. If the Code Editing window is not open, click on the View Code button in the Project window. At the top of the window, use the Object list to select the object whose code you want to view (in this case, select General) and use the Proc list to select the specific procedure (in this case, Declarations). Listing 11.1 General declarations in BABYEDITOR.FRM.
    Option Explicit

    ‘ True if the text being edited has changed.
    Dim TextChanged As Boolean

    ‘ True if a file has just been loaded.
    Dim JustLoaded As Boolean

    Dim FileName As String, OldName As String
Next, we will write the form’s Form_Load event procedure, which is triggered when the form is first loaded. With a single-form program such as this one, the form is loaded when the program begins execution. That makes this procedure the ideal place for program initialization code—code that does things like initializing variables and object properties. We have two tasks for Form_Load : setting the two flag variables to False and setting the Common Dialog control’s Filter property. I don’t want to explain the details of this property setting here, because I’ll be devoting a good deal of attention to the Common Dialog control later in the chapter. For now, try to restrain your curiosity. The code for the Form_Load procedure is presented in Listing 11.2. Listing 11.2 The Form_Load event procedure.
    Private Sub Form_Load()

    Dim Filter As String

    ‘ Clear flags.
    TextChanged = False
    JustLoaded = True

    ‘ Load filters into the common dialog box.
    Filter = “Text files (*.txt) | *.txt”
    Filter = Filter & “|Batch files (*.bat) | *.bat”
    Filter = Filter & “|INI files (*.ini) | *.ini”
    Filter = Filter & “|All files (*.*) | *.*”
    CommonDialog1.Filter = Filter

    End Sub
The next procedure, Form_Resize, has the job of setting the size and position of the Text Box control to fill the form. The Form_Resize event procedure is triggered every time the form’s size is changed, as well as when it is first displayed. The code for this procedure is shown in Listing 11.3. By setting the Text Box’s Top and Left properties to zero, it positions the Text Box against the top and left edges of the form. By setting the Text Box’s Width and Height properties to the values of the form’s ScaleWidth and ScaleHeight properties, the Text Box’s size is made equal to the form’s internal display area. Listing 11.3 The Form_Resize event procedure.
    Private Sub Form_Resize()

    ‘ Size the Text Box to fill the form.

    Text1.Top = 0
    Text1.Left = 0
    Text1.Width = ScaleWidth
    Text1.Height = ScaleHeight

    End Sub
Note that a form object also has Width and Height properties, but these differ from ScaleWidth and ScaleHeight. The former two properties give the outer dimensions of an object; in the case of a form object, these dimensions include its border, title bar, etc. The ScaleHeight and ScaleWidth properties refer specifically to the form’s display area—the area where you can place other objects. You’re probably wondering why we didn’t specify an object for the ScaleWidth and ScaleHeight properties. Why didn’t I write the code like this:
    Text1.Width = frmBabyEditor.ScaleWidth
    Text1.Height = frmBabyEditor.ScaleHeight
> In truth, I could have written it this way. The code would have worked fine; it just wasn’t necessary. In an object’s own event procedures, any reference to a property without an object name automatically refers to the object’s own properties. If you run the project now, you’ll see that you can enter text into the Text Box. You can also select text using the standard methods: dragging with the mouse or holding Shift while using the cursor movement keys. TIP: Getting “With” It The With statement can save you typing when you are working with several of an object’s properties. The general syntax is as follows:
>     With object
>         .Property1 = value1
>         .Property2 = value2
>         ...
>     End With
> Thus, to change several properties of a Text Box object named Text1, you could write:
>     With Text1
>         .Text = “Hello"
>         .Visible = True
>         .Height = 1500
>         .Width = 4000
>     End With
#### Implementing Cut, Copy, And Paste The first requirement for the Cut, Copy, and Paste operations is taken care of for us by the Text Box’s built-in capabilities. As we’ll soon see, so are most of the other parts. During program execution, the user can select text in the Text Box using the standard Windows techniques. The Text Box control has a property named SelLength that gives us the length (in characters) of the selected text in the Text Box, and another property named SelText that returns the selected text itself. It’s a trivial matter to see if any text is selected (simply verify that SelLength is greater than zero) and, if so, to retrieve it.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.